package com.fjzx.androidframe.base.activity;

import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.fjzx.androidframe.base.R;
import com.fjzx.androidframe.base.annotation.injector.ContentViewInjector;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 基础Activity类，不带数据请求器
 *
 * @author xiongyongcong
 */
public abstract class BaseSimpleActivity extends AppCompatActivity {
    protected ProgressDialog mProgress;
    protected Toolbar mToolbar;
    protected Unbinder mUnbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ARouter.getInstance().inject(this);
        super.setContentView(R.layout.activity_base);
        bindContentView();
        mUnbinder = ButterKnife.bind(this);
        initSystemBar();
        initView();
    }

    protected void bindContentView() {
        setContent(ContentViewInjector.inject(this));
    }

    @Override
    protected void onDestroy() {
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
        super.onDestroy();
    }

    public void setContent(int layoutResID) {
        FrameLayout mainContent = findViewById(R.id.main_content);
        LayoutInflater.from(this).inflate(layoutResID, mainContent, true);
    }

    /**
     * 初始化TitleBar
     */
    protected void initSystemBar() {
        mToolbar = findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(view -> onBackPressed());
        try {
            ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(), 0);
            if (activityInfo.labelRes > 0) {
                mToolbar.setTitle(activityInfo.labelRes);
            }
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 显示Toast
     *
     * @param msg
     */
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }


    public void showToast(int msgRes) {
        Toast.makeText(this, msgRes, Toast.LENGTH_SHORT).show();
    }

    /**
     * 显示进度对话框
     *
     * @param msg
     */
    public void showProgress(String msg) {
        if (mProgress == null) {
            mProgress = new ProgressDialog(this);
            mProgress.setIndeterminate(true);
            mProgress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        if (!TextUtils.isEmpty(msg)) {
            mProgress.setMessage(msg);
        }
        mProgress.show();
    }

    public void showProgress(int msg) {
        if (msg > 0) {
            showProgress(getString(msg));
        }
    }

    /**
     * 显示进度对话框，不可取消
     *
     * @param msg
     */
    public void showUnCancelProgress(String msg) {
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        if (!TextUtils.isEmpty(msg)) {
            dialog.setMessage(msg);
        }
        dialog.setCancelable(false);
        dialog.show();
    }

    /**
     * 隐藏对话框
     */
    public void hideProgress() {
        if (mProgress != null && mProgress.isShowing()) {
            mProgress.dismiss();
        }
    }

    /**
     * 子类重写此方法，初始化View
     */
    abstract protected void initView();
}
